/*
 注意:define定义的名称应与文件名保持一致
 AM.controller.Dicts 就可以理解成在controller目录下的Dicts.js文件
 AM 在/web/js/dict.js文件中通过appFolder指定了根目录
 */

Ext.define('AM.controller.Dicts', {
	extend : 'Ext.app.Controller',
	stores : [ 'Dicts' ],//定义在mvc/store/Dicts.js
	models : [ 'Dict' ],//定义在mvc/model/Dict.js
	views : [ 'dict.Edit', 'dict.List' ],//mvc/view/dict/Edit.js List.js
	init : function() {
		this.control({
			'viewport > dictlist dataview' : {//为dictlist中的dataview组件添加双击事件
				itemdblclick : this.edit
			},
			'viewport > dictlist button[action=addNew]' : {
				//为dictlist中的button按钮(并且action属性的值为addNew)添加单击事件
				click : this.addNew
			},
			'viewport > dictlist button[action=delete]' : {
				click : this.Delete
			},
			'dictedit button[action=save]' : {
				click : this.update
			}
		});
	},

	addNew : function(button) {
		var edit = Ext.create('AM.view.dict.Edit').show();
		var rec = Ext.create('AM.model.Dict');
		//rec.set('id', "-1");
		edit.down('form').loadRecord(rec);
	},
	Delete : function(button) {
		var dv = button.up('grid'); 
        var selection = dv.getSelectionModel().getSelection()[0];
        if (selection) {
            this.getDictsStore().remove(selection);
    		this.getDictsStore().sync();
        }
	},
	edit : function(grid, record) {
		var edit = Ext.create('AM.view.dict.Edit').show();

		edit.down('form').loadRecord(record);
	},

	update : function(button) {
		var win = button.up('window'), form = win.down('form'), record = form
				.getRecord(), values = form.getValues();
		
		record.set(values);
		if(record.get("id")==""){
			record.setId(null);
			this.getDictsStore().insert(0, record);
		}
		
		win.close();
		this.getDictsStore().sync();
	}
});
